<html xmlns:th="http://www.thymeleaf.org"
      xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity4">
<body>
<form id="roleForm"  role="form" method="post" action="role/savePrivilege">
    <div class="row">
        <div class="col-md-7">
            <input type="hidden" name="id" id="roleId" th:value="${entity.id}" />
            <div class="form-group" >
                <label for="name">权限代码:</label>
                <input type="text" class="form-control" name="name" id="name"th:value="${entity.name}"  disabled/>
            </div>
            <div class="form-group" >
                <label for="roleName">权限名称:</label>
                <input type="text" class="form-control" name="roleName"  id="roleName" th:value="${entity.roleName}" disabled />
            </div>
        </div>
        <div class="col-md-5" style="margin-top: 10px;">
            <input type="hidden" id="treeArray" name="treeArray" />
            <ul id="roleZtree" class="ztree" style="width:200px; overflow:auto;"></ul>
        </div>
    </div>
</form>
<script th:inline="javascript">
    <![CDATA[
    $(function () {
        // 绑定dialog的确定按钮的监听事件
        $("#btnOk",window.top.document).click(function() {
            var t = $.fn.zTree.getZTreeObj("roleZtree");
            if(t == null ){
                window.Ewin.alert({message:'您当前还没有权限进行此操作!'})
                return;
            }
            var nodes = t.getCheckedNodes(true);
            var treeArray = "";
            for(var i=0;i<nodes.length;i++){
                if(i==0){
                    treeArray = nodes[i].id
                }else{
                    treeArray =  treeArray + "," + nodes[i].id
                }
            }
            // $("#treeArray").attr("value",treeArray);

            var url = $("#roleForm", window.top.document).attr('action');
            var data = JSON.stringify({
                "id":$("#roleId").val(),
                "name": $("#name").val(),
                "roleName": $("#roleName").val(),
               "treeArray":treeArray
            });
            $.ajax({
                contentType:
                    "application/json",
                type:
                    'POST',
                url: url,
                data: data,
                success:
                    function (res) {
                        if(res.result){
                            // $('.modal-dialog', window.top.document).parent('div').remove()
                            // $('body', window.top.document).find('.modal-backdrop').remove();
                            // jquery 调用刷新当前操作的table页面的refresh方法
                            // $(window.parent.document).contents().find(".tab-pane.fade.active.in iframe")[0].contentWindow.doQuery();
                            activeTabBodyRefresh();
                            // window.Ewin.alert({message:'修改数据成功!'});
                            layer.msg(res.msg, {icon: 1, time: 2000})
                        }else{
                            window.Ewin.alert({message:res.msg});
                        }
                    },
                error: function (status) {
                    window.Ewin.alert({message: status.status + ':修改数据失败!'});
                }
            });

        });


        var setting = {
            check: {
                enable: true
            },
            view: {
                dblClickExpand: false,
                showLine: true,
                selectedMulti: false
            },
            data: {
                simpleData: {
                    enable:true,
                    idKey: "id",
                    pIdKey: "pId",
                    rootPId: "0"
                }
            },
            callback: {
                beforeClick: function(treeId, treeNode) {
                    var zTree = $.fn.zTree.getZTreeObj('roleZtree');
                    if (treeNode.isParent) {
                        zTree.expandNode(treeNode);
                        return false;
                    } else {
                        return true;
                    }
                }
            }
        };

        $.post("./role/loadRoleWriteTree?id="+[[${entity.id}]],function(info){
            var t = $("#roleZtree");
            t = $.fn.zTree.init(t, setting,info.data);
        })
    })
    ]]>
</script>
<script th:src="@{/layer/layer.js}"></script>
</body>
</html>